//
//  BDShopEditCommondViewController.m
//  BadBoy
//
//  Created by wen on 2017/3/24.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopEditCommondViewController.h"
#import "YYStarView.h"
#import "BDTextView.h"
#import "BDUserModel.h"
#import "BDTishiViewController.h"
#import "BDShopDetailModel.h"
@interface BDShopEditCommondViewController ()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *topContentView;
@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, strong) UILabel *shopNameLabel;
@property (nonatomic, strong) UILabel *sortLabel;
@property (nonatomic, strong) UILabel *commenNumberLabel;

@property (nonatomic, strong) UIView *bottomContentView;
@property (nonatomic, strong) YYStarView *startView;
@property (nonatomic, strong) BDTextView *textView;

@property (nonatomic, strong) UIButton *fabuButton;
@property (nonatomic, strong) BDShopDetailModel *detailModel;

@end

@implementation BDShopEditCommondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"写评论");
    [self requestDatasource];
}

- (void)requestDatasource{
    [MBManager showLoadingAlert:ASLocalizedString(@"加载中")clickHidden:NO clickBlock:nil];
    NSString *headerVale = [BDUserModel getHeaderValidation];
    NSString *url = [KBDBaseURL stringByAppendingString:[NSString stringWithFormat:@"shop/%zd", self.couponsModel.shop_id]];
    
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headerVale automaticProcessingError:NO] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        [MBManager hideAlert];
        _detailModel = [BDShopDetailModel mj_objectWithKeyValues:x];
        [self addSubviews];
        [self yy_layoutSubviews];
        [self bindRAC];
    }];
    [signal subscribeError:^(NSError * _Nullable error) {
        [MBManager hideAlert];
        [[UIApplication sharedApplication].keyWindow alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"获取商家信息错误")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil clickBlock:^(NSInteger index) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}

- (void)addSubviews{
    [self.view addSubview:self.contentView];
    [self.view addSubview:self.fabuButton];
    
    [self.contentView addSubview:self.topContentView];
    [self.contentView addSubview:self.bottomContentView];
    
    
    [self.topContentView addSubview:self.coverImageView];
    [self.topContentView addSubview:self.shopNameLabel];
    [self.topContentView addSubview:self.sortLabel];
    [self.topContentView addSubview:self.commenNumberLabel];
    
    [self.bottomContentView addSubview:self.textView];
}

- (void)yy_layoutSubviews{
    self.topContentView.size = CGSizeMake(kBDScreenWidth, 87);
    self.topContentView.layer.borderColor = kBDColorGray.CGColor;
    self.topContentView.layer.borderWidth = 1;
    self.topContentView.top = 0;
    self.topContentView.left = 0;
    self.topContentView.backgroundColor = [UIColor whiteColor];
    
    self.coverImageView.size = CGSizeMake(85, 67);
    self.coverImageView.left = 10;
    self.coverImageView.centerY = self.topContentView.height / 2.0;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.cover] placeholderImage:[UIImage imageNamed:@"icon"]];
    self.coverImageView.layer.masksToBounds = YES;
    self.coverImageView.layer.cornerRadius = 3;
    
    self.shopNameLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    self.shopNameLabel.font = [UIFont systemFontOfSize:16];
    self.shopNameLabel.text = self.detailModel.title;
    self.shopNameLabel.size = CGSizeMake([self.shopNameLabel.text widthWithFont:self.shopNameLabel.font], self.shopNameLabel.font.pointSize+1);
    self.shopNameLabel.left = self.coverImageView.right + 10;
    self.shopNameLabel.top = 24;
    
    self.sortLabel.textColor = [UIColor colorWithHexString:@"#ff8400"];
    self.sortLabel.font = [UIFont systemFontOfSize:13];
    NSString *sortString = [NSString stringWithFormat:ASLocalizedString(@"%zd 星级"), self.detailModel.score];
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:sortString];
    NSDictionary *attributeDict1 = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#767676"]};
    [attributedString1 addAttributes:attributeDict1 range:NSMakeRange(sortString.length - 2, 2)];
    self.sortLabel.attributedText = attributedString1;
    self.sortLabel.size = CGSizeMake([self.sortLabel.text widthWithFont:self.sortLabel.font], self.sortLabel.font.pointSize+1);
    self.sortLabel.left = self.shopNameLabel.left;
    self.sortLabel.top = self.shopNameLabel.bottom + 12;
    
    
    self.commenNumberLabel.textColor = [UIColor colorWithHexString:@"#ff8400"];
    self.commenNumberLabel.font = [UIFont systemFontOfSize:13];
    NSString *commentNumberString = [NSString stringWithFormat:ASLocalizedString(@"%zd 人评论"), self.detailModel.comment];
    NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:commentNumberString];
    NSDictionary *attributeDict2 = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#767676"]};
    [attributedString2 addAttributes:attributeDict2 range:NSMakeRange(commentNumberString.length - 3, 3)];
    self.commenNumberLabel.attributedText = attributedString2;
    self.commenNumberLabel.size = CGSizeMake([self.commenNumberLabel.text widthWithFont:self.commenNumberLabel.font], self.commenNumberLabel.font.pointSize+1);
    self.commenNumberLabel.left = self.sortLabel.right + 10;
    self.commenNumberLabel.top = self.sortLabel.top;
    
    
    UIView *litview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 40)];
    litview.layer.borderColor = kBDColorGray.CGColor;
    litview.layer.borderWidth = 1;
    [self.bottomContentView addSubview:litview];
    
    UILabel *pingfenLabel = [[UILabel alloc] init];
    pingfenLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    pingfenLabel.font = [UIFont systemFontOfSize:14];
    pingfenLabel.text = ASLocalizedString(@"商铺评分");
    pingfenLabel.size = CGSizeMake([pingfenLabel.text widthWithFont:pingfenLabel.font], pingfenLabel.font.pointSize+1);
    pingfenLabel.left = 10;
    pingfenLabel.centerY = litview.height / 2.0;
    [litview addSubview:pingfenLabel];
    
    [self.startView setStarWidth:20 starSpace:10 andDelegate:nil];
    self.startView.isIndicator = NO;
    self.startView.left = pingfenLabel.right + 10;
    self.startView.centerY = litview.height / 2.0;
    [litview addSubview:self.startView];
    
    UILabel *pinglunLabel = [[UILabel alloc] init];
    pinglunLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    pinglunLabel.font = [UIFont systemFontOfSize:14];
    pinglunLabel.text = ASLocalizedString(@"商铺评分");
    pinglunLabel.size = CGSizeMake([pinglunLabel.text widthWithFont:pinglunLabel.font], pinglunLabel.font.pointSize+1);
    pinglunLabel.left = 10;
    pinglunLabel.top = litview.bottom + 10;
    [self.bottomContentView addSubview:pinglunLabel];
    
    self.textView.size = CGSizeMake(kBDScreenWidth - 20, 150);
    self.textView.placeholder = ASLocalizedString(@"分享一下你的体验吧！");
    self.textView.textColor = [UIColor colorWithHexString:@"#767676"];
    self.textView.left = 10;
    [_textView setPlaceHolderFont:[UIFont systemFontOfSize:15]];
    self.textView.top = pinglunLabel.bottom + 10;
    
    self.bottomContentView.size = CGSizeMake(kBDScreenWidth, self.textView.bottom);
    self.bottomContentView.top = self.topContentView.bottom + 10;
    self.bottomContentView.left = 0;
    self.bottomContentView.backgroundColor = [UIColor whiteColor];
    self.bottomContentView.layer.borderColor = kBDColorGray.CGColor;
    self.bottomContentView.layer.borderWidth = 1;
    self.bottomContentView.layer.shadowColor = kBDColorGray.CGColor;
    self.bottomContentView.layer.shadowOffset = CGSizeMake(0, 0);
    self.bottomContentView.layer.shadowOpacity = 1;
    self.bottomContentView.layer.shadowRadius = 5;
    
    
    self.contentView.size = CGSizeMake(kBDScreenWidth, self.bottomContentView.bottom);
    self.contentView.top = kBDTopHeight + 10;
    self.contentView.left = 0;
    
    self.fabuButton.backgroundColor = KBDColorBlue;
    [self.fabuButton setTitle:ASLocalizedString(@"发布")forState:UIControlStateNormal];
    [self.fabuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.fabuButton.titleLabel.font = [UIFont systemFontOfSize:17];
    self.fabuButton.size = CGSizeMake(kBDScreenWidth - 20, 40);
    self.fabuButton.centerX = self.view.width / 2.0;
    self.fabuButton.top = self.contentView.bottom + 10;
    self.fabuButton.layer.masksToBounds = YES;
    self.fabuButton.layer.cornerRadius =2.5;
}

- (void)bindRAC{
    [[self.fabuButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSString *comment = self.textView.text;
        if (comment.length == 0) {
            [self.view alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"请填写评价内容")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil
                           clickBlock:nil];
        }
        else if (comment.length > 500) {
            [self.view alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"评论字数过长")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil
                           clickBlock:nil];
        }else{
            [MBManager showLoadingAlert:ASLocalizedString(@"请稍后")clickHidden:NO clickBlock:nil];
            NSString *url = [NSString stringWithFormat:@"%@shop/comment", KBDBaseURL];
            NSDictionary *para = @{@"id":@(self.detailModel.shop_id),
                                   @"comment":self.textView.text,
                                   @"score":@((NSInteger)[self.startView rating]),
                                   @"purchase_id":@(self.couponsModel.my_id)};
            NSString *headValid = [BDUserModel getHeaderValidation];
            RACCommand *commond = [BDNetworkingServer yy_POST:url parameters:para validation:headValid automaticProcessingError:NO];
            RACSignal *signal = [commond execute:nil];
            
            [signal subscribeNext:^(id  _Nullable x) {
                [MBManager hideAlert];
                [[UIApplication sharedApplication].keyWindow alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"评论发表成功")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil clickBlock:^(NSInteger index) {
                    self.couponsModel.status = BDMyCouponsStatusCommented;
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }];
            
            [signal subscribeError:^(NSError * _Nullable error) {
                [MBManager hideAlert];
                if (error.code == 40005) {
                    [[UIApplication sharedApplication].keyWindow alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"抱歉，此内容违反了《夜城社区管理规定(试行)》或相关法规政策，无法进行指定操作")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:@[ASLocalizedString(@"查看帮助")] clickBlock:^(NSInteger index) {
                        if (index == 1) {
                            BDTishiViewController *tishiVC = [[BDTishiViewController alloc] init];
                            tishiVC.xieyiType = BDXieyiTypePost;
                            [self.navigationController pushViewController:tishiVC animated:YES];
                        }
                    }];
                }else {
                    [MBManager showBriefAlert:ASLocalizedString(@"发表失败")clickHidden:NO clickBlock:nil];
                }
            }];
        }
    }];
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] init];
    }
    return _contentView;
}

- (UIView *)topContentView{
    if (!_topContentView) {
        self.topContentView = [[UIView alloc] init];
    }
    return _topContentView;
}

- (UIImageView *)coverImageView{
    if (!_coverImageView) {
        self.coverImageView = [[UIImageView alloc] init];
    }
    return _coverImageView;
}

- (UILabel *)shopNameLabel{
    if (!_shopNameLabel) {
        self.shopNameLabel = [[UILabel alloc] init];
    }
    return _shopNameLabel;
}

- (UILabel *)sortLabel{
    if (!_sortLabel) {
        self.sortLabel = [[UILabel alloc] init];
    }
    return _sortLabel;
}

- (UILabel *)commenNumberLabel{
    if (!_commenNumberLabel) {
        self.commenNumberLabel = [[UILabel alloc] init];
    }
    return _commenNumberLabel;
}

- (UIView *)bottomContentView{
    if (!_bottomContentView) {
        self.bottomContentView = [[UIView alloc] init];
    }
    return _bottomContentView;
}

- (YYStarView *)startView{
    if (!_startView) {
        self.startView = [[YYStarView alloc] init];
    }
    return _startView;
}

- (BDTextView *)textView{
    if (!_textView) {
        self.textView = [[BDTextView alloc] init];
    }
    return _textView;
}

- (UIButton *)fabuButton{
    if (!_fabuButton) {
        self.fabuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _fabuButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
